package zy.blue7.gui.demo;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @Author: blue7
 * @Date: 2022/2/24 22:19
 * @Version 1.0
 * @Description
 */
public class Client {

    public static void main(String[] args) throws InterruptedException {
        new Bootstrap()
                // 2.添加 EventLoop
                .group(new NioEventLoopGroup())
                //3. 选择客户端 channel实现
                .channel(NioSocketChannel.class)
                //4.添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override // todo 在建立链接的时候调用
                    protected void initChannel(NioSocketChannel ch) throws Exception {
//                        ch.pipeline().addLast(new StringEncoder());
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("------------------------:   channelRead");
                            }

                            @Override
                            public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelRegistered");
                                super.channelRegistered(ctx);
                            }

                            @Override
                            public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelUnregistered");
                                super.channelUnregistered(ctx);
                            }

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelActive");
                                super.channelActive(ctx);
                            }

                            @Override
                            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelInactive");
                                super.channelInactive(ctx);
                            }

                            @Override
                            public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelReadComplete");
                                super.channelReadComplete(ctx);
                            }

                            @Override
                            public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                                System.out.println("------------------------:   userEventTriggered");
                                super.userEventTriggered(ctx, evt);
                            }

                            @Override
                            public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
                                System.out.println("------------------------:   channelWritabilityChanged");
                                super.channelWritabilityChanged(ctx);
                            }

                            @Override
                            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                System.out.println("------------------------:   exceptionCaught");
                                super.exceptionCaught(ctx, cause);
                            }
                        });
                    }
                })
                //5.连接到服务器
                .connect(new InetSocketAddress("localhost", 8080))
                .sync() //todo 阻塞方法，知道链接建立
                .channel();
//                .writeAndFlush("nihao");


    }
}
